#include <stdlib.h>
#include "sikle.h"

struct HashNode {
  Word word;
  struct HashNode* next;
};

struct HashTable {
  HashNode* list[11];
};

HashTable* HashTable_new()
{
  int i;
  HashTable* table = (HashTable*)malloc(sizeof(HashTable));

  for(i = 0; i < 11; i++) {
    table->list[i] = NULL;
  }

  return table;
}

void HashTable_put_word(HashTable* table, Word word)
{
  int hash = NString_hash(word.name);
  HashNode* node = NULL;

  for(node = table->list[hash]; node != NULL; node = node->next) {
    if(NString_equal(node->word.name, word.name)) { /* got a record */
      node->word = word;
      return;
    }
  }

  /* no record */
  node = (HashNode*)malloc(sizeof(HashNode));
  node->word = word;

  node->next = table->list[hash];
  table->list[hash] = node;
}

Word* HashTable_get_word(HashTable* table, NString name)
{
  int hash = NString_hash(name);
  HashNode* node = NULL;

  for(node = table->list[hash]; node != NULL; node = node->next) {
    if(NString_equal(node->word.name, name)) { /* got a record */
      return &(node->word);
    }
  }

  return NULL;
}
